package com.example.administrator.xiaoxiang.fragment.homedetailfragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.adapter.HomeContentAdapter;
import com.example.administrator.xiaoxiang.bean.HomeFragmentBaseBean;
import com.example.administrator.xiaoxiang.bean.HomeFragmentJsonBean;
import com.example.administrator.xiaoxiang.bean.HomeGoodGroupItem;
import com.example.administrator.xiaoxiang.bean.HomeGoodGroups;
import com.example.administrator.xiaoxiang.utils.AsyncTaskHelper;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/7.
 * 首页从第2个页面至第7个页面的基类
 */

public abstract class HomeCommonFragment extends Fragment
implements AsyncTaskHelper.OnDataDownloadListener,SwipeRefreshLayout.OnRefreshListener{
    public AsyncTaskHelper helper;
    private List<HomeFragmentBaseBean> data;
    private HomeContentAdapter adapter;
    private ListView listView;
    private View view;
    private boolean flag;
    private SwipeRefreshLayout swipeRefreshLayout;//下拉刷新

    public HomeCommonFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_home_common, null);
        init(view);
        loadData();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return view;
    }

    void loadData(){}//由子类进行实现

    private void init(View view) {
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipRefresh_home_common);
        listView = (ListView) view.findViewById(R.id.listView_common_home_fragment);
        helper = new AsyncTaskHelper();
        data = new ArrayList<>();
        adapter = new HomeContentAdapter(data,getContext());
        listView.setAdapter(adapter);

        flag = false;
        swipeRefreshLayout.setRefreshing(true);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setDistanceToTriggerSync(200);
        swipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        swipeRefreshLayout.setColorSchemeColors(Color.RED);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
    }

    @Override
    public void onDataDownload(byte[] result) {
        String json = new String(result);
//        Log.e("dd", "onDataDownload: "+json );
        Gson gson = new Gson();
        HomeFragmentJsonBean homeFragmentJsonBean = gson.fromJson(json, HomeFragmentJsonBean.class);
        //精品分组
        for (int i = 0; i < homeFragmentJsonBean.getBody().getGoodGroups().size(); i++) {
            HomeGoodGroups homeGoodGroups = new HomeGoodGroups();
            homeGoodGroups.thumbnailUrl = homeFragmentJsonBean.getBody().getGoodGroups().get(i).getSubject().getThumbnailUrl();
            homeGoodGroups.id = homeFragmentJsonBean.getBody().getGoodGroups().get(i).getSubject().getId()+".json";
            for (int j = 0; j < homeFragmentJsonBean.getBody().getGoodGroups().get(i).getSlideGoods().size(); j++) {
                HomeGoodGroupItem homeGoodGroupItem = new HomeGoodGroupItem();
                homeGoodGroupItem.subTitle = homeFragmentJsonBean.getBody().getGoodGroups().get(i).getSlideGoods().get(j).getSubTitle();
                homeGoodGroupItem.peroidInstalmentAmount = homeFragmentJsonBean.getBody().getGoodGroups().get(i).getSlideGoods().get(j).getPeroidInstalmentAmount();
                homeGoodGroupItem.instalmentPeriods = homeFragmentJsonBean.getBody().getGoodGroups().get(i).getSlideGoods().get(j).getInstalmentPeriods()+"";
                homeGoodGroupItem.directPaymentAmount = homeFragmentJsonBean.getBody().getGoodGroups().get(i).getSlideGoods().get(j).getDirectPaymentAmount();
                homeGoodGroupItem.thumbnailUrl = homeFragmentJsonBean.getBody().getGoodGroups().get(i).getSlideGoods().get(j).getThumbnailUrl();
                homeGoodGroupItem.goodsCode = homeFragmentJsonBean.getBody().getGoodGroups().get(i).getSlideGoods().get(j).getGoodsCode();
                homeGoodGroups.list.add(homeGoodGroupItem);
            }
            data.add(homeGoodGroups);
        }
//        adapter = new HomeContentAdapter(data,getContext());
        if (swipeRefreshLayout.isRefreshing()&&flag) {
            for (int i = 0; i < data.size() / 2; i++) {
                data.remove(i);
            }
        }
        adapter.notifyDataSetChanged();
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        loadData();
//        adapter.notifyDataSetChanged();
        flag = true;
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void onPause() {
        super.onPause();
        swipeRefreshLayout.setRefreshing(false);
    }
}
